package flower.home.common.poi;

import cn.abellflower.utils.EwmUtil;
import com.google.gson.GsonBuilder;
import flower.home.common.poi.bo.ExcelDataBo;
import flower.home.common.poi.exception.PoiException;
import org.apache.commons.collections.CollectionUtils;
import org.apache.poi.openxml4j.exceptions.OpenXML4JException;
import org.apache.poi.openxml4j.opc.OPCPackage;
import org.apache.poi.xssf.eventusermodel.XSSFReader;
import org.apache.poi.xssf.model.StylesTable;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.XMLReader;

import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintStream;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
 * @author gbzdyf@126.com
 * @Create: 2020/4/16 10:29
 */
public class PoiMainUtils {

    private static final GsonBuilder gb;

    static {
        gb = new GsonBuilder();
    }

    public static void main(String[] args) throws OpenXML4JException, ParserConfigurationException, SAXException, IOException {
        InputStream inputStream = PoiMainUtils.class.getResourceAsStream("/poi/template/ScanKpTemp_2021-06-17.xlsx");
        List<String[]> excelRow = transInputStream(inputStream, DataTempEnum.getEnumsColumnLength());
//        String va = gb.create().toJson(excelRow.subList(1, excelRow.size()));
        System.out.println("--------------------------------------------------------------------------------------");
//        System.out.println(String.format("{%s}", va));
        System.out.println("--------------------------------------------------------------------------------------");
        int offset = 2;
        List<ExcelDataBo> excelDataBos = ExcelDataBo.transExcelDataBoList(excelRow.subList(offset + 1, excelRow.size()));

        for (ExcelDataBo excelDataBo : excelDataBos) {
            if (BigDecimal.valueOf(50).equals(new BigDecimal(excelDataBo.getTaxAmount()))) {
                EwmUtil.createEwm(excelDataBo.getEwmUrl(),
                        "/Users/wkq/Documents/movie/20210617-ewm-" +
                                "913304017044065279" + "-" + excelDataBo.getTaxAmount() + "/",
                        "913304017044065279" + "_" + excelDataBo.getOrderNo());
            } else {
                EwmUtil.createEwm(excelDataBo.getEwmUrl(),
                        "/Users/wkq/Documents/movie/20210617-ewm-" +
                                "913304017044065279" + "-" + excelDataBo.getTaxAmount() + "/",
                        "913304017044065279" + "_" + excelDataBo.getOrderNo());
            }
        }
    }

    /**
     * 读取数据
     * @return
     */
    public static List<String[]> transInputStream(InputStream inputStream, Integer columnLength) throws IOException,
            OpenXML4JException, ParserConfigurationException, SAXException {
        OPCPackage p = OPCPackage.open(inputStream);
        List<String[]> process = process(p, System.out, columnLength);
        if (CollectionUtils.isEmpty(process)) {
            throw new PoiException(300, "模板错误，请重新下载正确模板！");
        }
        return process;
    }

    /**
     * 初始化这个处理程序 将
     *
     * @throws IOException
     * @throws OpenXML4JException
     * @throws ParserConfigurationException
     * @throws SAXException
     */
    private static List<String[]> process(OPCPackage pkg, PrintStream output, int minColumns)
            throws IOException, OpenXML4JException, ParserConfigurationException, SAXException {
        ReadOnlySharedStringsTable readOnlySharedStringsTable = new ReadOnlySharedStringsTable(pkg);
        XSSFReader xssfReader = new XSSFReader(pkg);
        List<String[]> list = new ArrayList<>();
        StylesTable styles = xssfReader.getStylesTable();
        XSSFReader.SheetIterator iter = (XSSFReader.SheetIterator) xssfReader.getSheetsData();
        while (iter.hasNext()) {
            InputStream stream = iter.next();
            list = processSheet(styles, readOnlySharedStringsTable, stream, output, minColumns);
            stream.close();
        }
        return list;
    }

    /**
     * Parses and shows the content of one sheet using the specified styles and
     * shared-strings tables.
     *
     * @param styles
     * @param readOnlySharedStringsTable
     * @param sheetInputStream
     */
    private static List<String[]> processSheet(StylesTable styles, ReadOnlySharedStringsTable readOnlySharedStringsTable,
                                        InputStream sheetInputStream, PrintStream output, int minColumns)
            throws IOException, ParserConfigurationException, SAXException {
        InputSource sheetSource = new InputSource(sheetInputStream);
        SAXParser saxParser = SAXParserFactory.newInstance().newSAXParser();
        XMLReader sheetParser = saxParser.getXMLReader();
        XSSFSheetHandler handler = new XSSFSheetHandler(styles, readOnlySharedStringsTable, minColumns, output);
        sheetParser.setContentHandler(handler);
        sheetParser.parse(sheetSource);
        return handler.getRows();
    }
}
